﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Bll;


namespace DBA.Web.SystemManage.EmployeeManage
{
    public partial class EmployeeShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlBind();
                lstMultipleValues.Attributes.Add("onclick", "FindSelectedItems(this," + txtSelectedMLValues.ClientID + ");");
                SetMultiValue();
                if (!String.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = RequestHelper.QueryString("id");
                    ModifyEmployeeInit();
                }
            }


        }

        protected void SetMultiValue()
        {
            string strSql = "select * from Sys_RoleInfo order by RoleID desc";
            Data.ADO.DataAccessLibrary dal = new DataAccessLibrary();
            lstMultipleValues.DataSource = dal.ExecuteFillDataTable(strSql);
            lstMultipleValues.DataTextField = "RoleName";
            lstMultipleValues.DataValueField = "RoleID";
            lstMultipleValues.DataBind();

            //for (int i = 0; i < lstMultipleValues.Items.Count; i++)
            //{
            //    if (lstMultipleValues.Items[i].Value == "7")
            //        lstMultipleValues.Items[i].Selected = true;
            //}
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            DBA.Model.EmployeeInfo emp = getNewModel();
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            if (hidID.Value.Equals(string.Empty))//添加角色
            {

                string rtnValue = dal.ModifyData.InsertGetID<DBA.Model.EmployeeInfo>(emp).ToString();
                InsertUserRole(rtnValue);
                hdResult.Value = dal.ErrorMessage.Equals(string.Empty) ? "1" : "0";
            }
            else
            {
                if (dal.ModifyData.Update<DBA.Model.EmployeeInfo>(emp) == "0")
                {

                    hdResult.Value = "0";
                }
                else
                {
                    DeleteUserRole(emp.EmployeeID.ToString());
                    InsertUserRole(emp.EmployeeID.ToString());
                    hdResult.Value = "1";
                }
            }
        }

        protected void ddlBind()
        {
            //绑定性别
            ddlSex.Items.Add(new ListItem("男", "0"));
            ddlSex.Items.Add(new ListItem("女", "1"));
            ddlSex.DataBind();

            ddlEdu.Items.Add(new ListItem("—请选择—", "-1"));
            ddlEdu.Items.Add(new ListItem("本科", "1"));
            ddlEdu.Items.Add(new ListItem("硕士研究生", "2"));
            ddlEdu.Items.Add(new ListItem("博士研究生", "3"));
            ddlEdu.DataBind();

            BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系
            BaseCodeDDL.DdlInit(ddlSuo, "Suo");
            BaseCodeDDL.DdlInit(ddlDept, "sys_department");
            BaseCodeDDL.DdlInit(ddlJszwzg, "JSZWZG");
            BaseCodeDDL.DdlInit(ddlEduAttainments, "EduAttainments");
            BaseCodeDDL.DdlInit(ddlXzzw, "PartyDuty");

        }

        protected DBA.Model.EmployeeInfo getNewModel()
        {

            DBA.Model.EmployeeInfo employee = new Model.EmployeeInfo();
            employee.EmployeeID = hidID.Value.Equals(string.Empty) ? 0 : int.Parse(hidID.Value);
            employee.Name = txtName.Value.Trim();
            employee.SalaryID = txtSalaryID.Value.Trim();

            employee.Sex = ddlSex.SelectedValue == "0" ? true : false;
            employee.Password = txtPwd.Text.Trim();
            employee.Xi_ID = int.Parse(ddlXi.SelectedValue);
            employee.Suo_ID = int.Parse(ddlSuo.SelectedValue);
            if (txtBirthDay.Value != "")
            {
                employee.BirthDay = DateTime.Parse(txtBirthDay.Value);
            }

            if (txtjszwzgDate.Value != "")
            {
                employee.JszwzgTime = DateTime.Parse(txtjszwzgDate.Value);
            }

            employee.JszwzgID = int.Parse(ddlJszwzg.SelectedValue);
            employee.JszwzgClass = txtJszwjgClass.Value.Trim();
            employee.Degree = int.Parse(ddlEdu.SelectedValue);
            employee.EaID = int.Parse(ddlEduAttainments.SelectedValue);
            employee.PD_id = int.Parse(ddlXzzw.SelectedValue);
            employee.Dpt_id = int.Parse(ddlDept.SelectedValue);

            if (txtInSchoolDate.Value != "")
            {
                employee.SchoolTime = DateTime.Parse(txtInSchoolDate.Value);
            }
            if (txtWorkTime.Value != "")
            {
                employee.JoinWorkTime = DateTime.Parse(txtWorkTime.Value.Trim());
            }
            if (txtJoinPartTime.Value != "")
            {
                employee.JoinPartTime = DateTime.Parse(txtJoinPartTime.Value.Trim());
            }
            employee.Political = txtPolitical.Value.Trim();
            employee.TeacherClass = txtTeacherClass.Value.Trim();

            employee.Speciality = txtSpeciality.Value.Trim();
            if (txtGraduteTime.Value != "")
            {
                employee.GraduteTime = DateTime.Parse(txtGraduteTime.Value.Trim());
            }
            employee.GtaduteSchool = txtGtaduteSchool.Value.Trim();
            employee.Bachelor = txtBachelor.Value.Trim();
            if (txtBachelorTime.Value != "")
            {
                employee.BachelorTime = DateTime.Parse(txtBachelorTime.Value.Trim());
            }
            employee.Reserch = txtReserch.Value.Trim();
            employee.Country = txtCountry.Value.Trim();

            employee.HomeTown = txtHomeTown.Value.Trim();
            employee.IDNumber = txtIDNumber.Value.Trim();
            employee.PhoneNum = txtPhoneNum.Value.Trim();
            employee.Eamil = txtEamil.Value.Trim();
            employee.SubjectPartTime = txtSubjectPartTime.Value.Trim();

            return employee;
        }

        protected void ModifyEmployeeInit()
        {
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            string id = hidID.Value;
            if (!VerificationHelper.IsNullOrEmpty<string>(hidID.Value))
            {
                DBA.Model.EmployeeInfo employee = dal.ModifyData.GetEntity<DBA.Model.EmployeeInfo>(id);
                txtName.Value = employee.Name;
                txtSalaryID.Value = employee.SalaryID;
                ddlSex.SelectedValue = employee.Sex ? "0" : "1";
                //employee.Password = "111111";
                ddlXi.SelectedValue = employee.Xi_ID.ToString();
                ddlSuo.SelectedValue = employee.Suo_ID.ToString();
                txtBirthDay.Value = BaseCodeDDL.ConvertTime(employee.BirthDay).ToString();

                txtjszwzgDate.Value = BaseCodeDDL.ConvertTime(employee.JszwzgTime).ToString();

                ddlJszwzg.SelectedValue = employee.JszwzgID.ToString();
                txtJszwjgClass.Value = employee.JszwzgClass;
                ddlEdu.SelectedValue = employee.Degree.ToString();
                ddlEduAttainments.SelectedValue = employee.EaID.ToString();
                ddlXzzw.SelectedValue = employee.PD_id.ToString();
                ddlDept.SelectedValue = employee.Dpt_id.ToString();
                txtPwd.Attributes.Add("value", employee.Password);


                txtInSchoolDate.Value = BaseCodeDDL.ConvertTime(employee.SchoolTime).ToString();


                txtWorkTime.Value = BaseCodeDDL.ConvertTime(employee.JoinWorkTime).ToString();


                txtJoinPartTime.Value = BaseCodeDDL.ConvertTime(employee.JoinPartTime).ToString();

                txtPolitical.Value = employee.Political;
                txtTeacherClass.Value = employee.TeacherClass;

                txtSpeciality.Value = employee.Speciality;

                txtGraduteTime.Value = BaseCodeDDL.ConvertTime(employee.GraduteTime).ToString();

                txtGtaduteSchool.Value = employee.GtaduteSchool;
                txtBachelor.Value = employee.Bachelor;

                txtBachelorTime.Value = BaseCodeDDL.ConvertTime(employee.BachelorTime).ToString();

                txtReserch.Value = employee.Reserch;
                txtCountry.Value = employee.Country;

                txtHomeTown.Value = employee.HomeTown;
                txtIDNumber.Value = employee.IDNumber;
                txtPhoneNum.Value = employee.PhoneNum;
                txtEamil.Value = employee.Eamil;
                txtSubjectPartTime.Value = employee.SubjectPartTime;


                //用户角色的选中
                string strSql = "select RoleID from Sys_RoleAndUser  where employeeid = '" + id + "'";
                DataTable dtRole = dal.ExecuteFillDataTable(strSql);
                string roleName = string.Empty;
                foreach (DataRow dr in dtRole.Rows)
                {
                    for (int i = 0; i < lstMultipleValues.Items.Count; i++)
                    {
                        if (lstMultipleValues.Items[i].Value == dr["RoleID"].ToString())
                        {
                            lstMultipleValues.Items[i].Selected = true;
                            roleName += lstMultipleValues.Items[i].Text + ",";
                        }
                    }
                    txtSelectedMLValues.Value = roleName.TrimEnd(',');
                }
            }
        }

        protected bool DeleteUserRole(string id)
        {
            string strSql = "Delete from Sys_RoleAndUser where EmployeeID='" + id + "'";
            DataAccessLibrary dal = new DataAccessLibrary();
            dal.ExecuteNonQuery(strSql);

            return dal.ErrorMessage.Equals(string.Empty);
        }

        protected void InsertUserRole(string id)
        {

            string strSql = "Insert into Sys_RoleAndUser (EmployeeID,RoleID) values (@EmployeeID,@RoleID) ";
            DataAccessLibrary dal = new DataAccessLibrary();
            for (int i = 0; i < lstMultipleValues.Items.Count; i++)
            {
                if (lstMultipleValues.Items[i].Selected)
                {
                    dal.ManagerParameter.ClearParameter();
                    dal.ManagerParameter.AddNewInParameter("EmployeeID", id);
                    dal.ManagerParameter.AddNewInParameter("RoleID", lstMultipleValues.Items[i].Value);
                    dal.ExecuteNonQuery(strSql);
                }
            }
        }
    }
}